/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2025 DBeaver Corp and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ext.cubrid.edit;

import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.ext.cubrid.model.CubridDataSource;
import org.jkiss.dbeaver.ext.cubrid.model.CubridProcedure;
import org.jkiss.dbeaver.ext.cubrid.model.CubridUser;
import org.jkiss.dbeaver.ext.generic.edit.GenericProcedureManager;
import org.jkiss.dbeaver.ext.generic.model.GenericProcedure;
import org.jkiss.dbeaver.ext.generic.model.GenericStructContainer;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType;

import java.util.List;
import java.util.Map;

public class CubridProcedureManager extends GenericProcedureManager {

    @Override
    public boolean canCreateObject(@NotNull Object container) {
        CubridUser user = (CubridUser) container;
        CubridDataSource dataSource = (CubridDataSource) user.getDataSource();
        boolean isCurrentUser = user.getName().equalsIgnoreCase(dataSource.getCurrentUser());
        return isCurrentUser || !dataSource.isShard();
    }

    @Override
    public boolean canEditObject(GenericProcedure object) {
        return !((CubridDataSource) object.getDataSource()).isShard();
    }

    @Override
    public boolean canDeleteObject(GenericProcedure object) {
        return !((CubridDataSource) object.getDataSource()).isShard();
    }

    @Override
    protected GenericProcedure createDatabaseObject(
        @NotNull DBRProgressMonitor monitor,
        @NotNull DBECommandContext context,
        final Object container,
        Object from,
        Map<String, Object> options
    ) {
        String type = options.get("container").toString();
        DBSProcedureType procedureType = type.equals("Functions")
            ? DBSProcedureType.FUNCTION : DBSProcedureType.PROCEDURE;
        return new CubridProcedure((GenericStructContainer) container, procedureType);
    }

    @Override
    protected void addObjectCreateActions(
        @NotNull DBRProgressMonitor monitor,
        @NotNull DBCExecutionContext executionContext,
        List<DBEPersistAction> actions,
        ObjectCreateCommand command,
        @NotNull Map<String, Object> options
    ) throws DBCException {
        CubridProcedure procedure = (CubridProcedure) command.getObject();
        actions.add(new SQLDatabasePersistAction("Create Procedure", procedure.getSource()));
    }

    @Override
    protected void addObjectModifyActions(
        @NotNull DBRProgressMonitor monitor,
        @NotNull DBCExecutionContext executionContext,
        @NotNull List<DBEPersistAction> actionList,
        @NotNull ObjectChangeCommand objectChangeCommand,
        @NotNull Map<String, Object> options
    ) {
        CubridProcedure procedure = (CubridProcedure) objectChangeCommand.getObject();
        actionList.add(new SQLDatabasePersistAction("Modify Procedure", procedure.getSource()));
    }
}
